package boogui.movie;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import boogui.db.BooguiDAO;
import boogui.db.IBooguiDAO;

public class MovieSelectDAO implements IMovieSelectDAO {
	IBooguiDAO bdao = null;
	MovieSQL mvsql = null;

	public MovieSelectDAO() {
		bdao = new BooguiDAO();
		mvsql = new MovieSQL();
		
	}

	@Override
	public List<MovieVO> movieTitle() {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		List<MovieVO> result = new ArrayList<>();
		try {
			conn = bdao.openDatabase();
			pstmt = conn.prepareStatement(mvsql.getTitleSQL());
			rs = pstmt.executeQuery();
			while(rs.next()){
				MovieVO m = new MovieVO();
				m.setMovtitle(rs.getString("title"));
				result.add(m);
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			bdao.closeDatabase(conn, pstmt, rs);
		}

		return result;

	}

	@Override
/*	private String locationSQL = "select distinct location"
			+ " from showing s inner join movies m"
			+ " on s.movno = m.movno inner join cinema c"
			+ " on c.cineno = s.cineno where title = ?";
	*/
	public List<MovieAreaVO> movieArea(MovieVO m) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		List<MovieAreaVO> result = new ArrayList<>();
		
		try{
			conn = bdao.openDatabase();
			pstmt = conn.prepareStatement(mvsql.getLocationSQL());	
			pstmt.setString(1, m.getMovtitle());
			
			rs=pstmt.executeQuery();
			
			while(rs.next()){
				MovieAreaVO mv = new MovieAreaVO();			
				mv.setLocation(rs.getString("location"));
				result.add(mv);
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			bdao.closeDatabase(conn, pstmt, rs);
		}		
		return result;
	}
	
	@Override
	public List<MovieAreaVO> movieCinema(MovieVO m,MovieAreaVO ma) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		List<MovieAreaVO> result = new ArrayList<>();
		
/*		private String cinemaSQL = "select distinct cinename, theater"
				+ " from showing s inner join movies m"
				+ " on s.movno = m.movno inner join cinema c"
				+ " on c.cineno = s.cineno"
				+ " where title = ? and location = ?";*/
		try{
			conn = bdao.openDatabase();
			pstmt = conn.prepareStatement(mvsql.getCinemaSQL());	
			pstmt.setString(1, m.getMovtitle());
			pstmt.setString(2,ma.getLocation());
			rs=pstmt.executeQuery();
			
			while(rs.next()){
				
				ma.setTheater(rs.getString("theater"));
				ma.setCinename(rs.getString("cinename"));
				result.add(ma);
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			bdao.closeDatabase(conn, pstmt, rs);
		}		
		return result;
	}

	
}
